package se.webbzon.boltzmann.editor.component.sequence.panel;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;

import se.webbzon.boltzmann.game.sequence.builder.condition.ConditionRow;
import se.webbzon.boltzmann.game.sequence.builder.condition.ConditionEnum;

public class ConditionRowPanel extends AbstractRowPanel {
	
	// Auto generated serial version UID
	private static final long serialVersionUID = -8402240262555999793L;

	// The combo boxes of this panel
	private final JComboBox<ConditionEnum> conditionInput;
	
	// The text fields of this panel
	private final JTextField lhInput, rhInput;
	
	/*============================================================
	Constructors
	============================================================*/

	/** Creates a new condition row panel. **/
	public ConditionRowPanel(ConditionRow row) {
		super(row);
		setLayout(new GridLayout(3,2));
		
		add(new JLabel("Condition:"));
		conditionInput = 
				new JComboBox<ConditionEnum>(ConditionEnum.values());
		conditionInput.setSelectedItem(row.getCondition());
		conditionInput.addActionListener(new ActionListenerImpl());
		add(conditionInput);
		
		add(new JLabel("Left Hand Argument:"));
		lhInput = new JTextField(row.getLeftHandArgument());
		add(lhInput);
		
		add(new JLabel("Right Hand Argument:"));
		rhInput = new JTextField(row.getRightHandArgument());
		add(rhInput);
		
		updateArguments(row.getCondition().arguments());
	}

	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public boolean apply(Collection<String> errorMessages) {
		ConditionRow row = (ConditionRow) getSequenceRow();
		ConditionEnum condition = 
				(ConditionEnum) conditionInput.getSelectedItem();
		row.setCondition(condition);
		
		int args = condition.arguments();
		if (args == 0) {
			row.setLeftHandArgument("");
			row.setRightHandArgument("");
		} else if (args == 1) {
			row.setLeftHandArgument(lhInput.getText());
			row.setRightHandArgument("");
		} else {
			row.setLeftHandArgument(lhInput.getText());
			row.setRightHandArgument(rhInput.getText());
		}
		
		return true;
	}
	
	/*============================================================
	Private Methods
	============================================================*/
	
	/** Updates the state of the argument input fields. **/
	private final void updateArguments(int arguments) {
		if (arguments == 0) {
			lhInput.setEditable(false);
			rhInput.setEnabled(false);
		} else if (arguments == 1) {
			lhInput.setEditable(true);
			rhInput.setEnabled(false);
		} else {
			lhInput.setEditable(true);
			rhInput.setEnabled(true);
		}
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	/** An implementation of an action listener. **/
	private final class ActionListenerImpl implements ActionListener {

		@Override public void actionPerformed(ActionEvent arg0) {
			int args = ((ConditionEnum) conditionInput.getSelectedItem()).arguments();
			updateArguments(args);
		}
		
	}

}
